﻿Public Class LightRequestHandler
    Implements System.Web.IHttpHandler

    Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest

        System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf Analytics))

        If (context.Request.InputStream.Length <= 0) Then
            context.Response.Write("It works!")
            context.Response.End()
        End If

        Dim json As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Light.Common.Request))
        Dim r As Light.Common.Request = json.ReadObject(context.Request.InputStream)

        Dim reg As New Registry
        Dim response As Light.Common.Response = Nothing
        Try
            response = reg.ProcessRequest(r)
        Catch ex As Exception
            If (response Is Nothing) Then response = New Light.Common.Response
            response.ErrorMessage = ex.Message

        End Try

        Dim jsonResponse As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Light.Common.Response))
        jsonResponse.WriteObject(context.Response.OutputStream, response)

    End Sub

    Public Sub Analytics(ByVal state As Object)
        Dim context As System.Web.HttpContext = CType(state, System.Web.HttpContext)
        'log the data

    End Sub

End Class
